package com.yinhe.music.yhmusic.dialog.collect;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.apkfuns.logutils.LogUtils;
import com.yinhe.music.type.person.BaseInfo;
import com.yinhe.music.yhmusic.R;
import com.yinhe.music.yhmusic.base.BaseModel;
import com.yinhe.music.yhmusic.base.BasePresenter;
import com.yinhe.music.yhmusic.base.BaseServiceDialogFragment;
import com.yinhe.music.yhmusic.dialog.EditDialog;
import com.yinhe.music.yhmusic.manager.CollectionDialogAdapter;
import com.yinhe.music.yhmusic.model.SongMenuList;
import com.yinhe.music.yhmusic.model.UserInfo;
import com.yinhe.music.yhmusic.network.response.Response;
import com.yinhe.music.yhmusic.widget.WrapContentLinearLayoutManager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 创建歌单弹窗
 */
public class CollectionDialog extends BaseServiceDialogFragment implements ICollectDialogContract.ICollectDialogView {

    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;

    private Context mContext;
    private List<Integer> mSongIdList;

    CollectDialogPresenter mPresenter;
    Unbinder unbinder;

    private int musicType;

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    public static CollectionDialog newInstance(ArrayList<Integer> mSongIdList, int musicType) {
        CollectionDialog fragment = new CollectionDialog();

        Bundle bundle = new Bundle();
        bundle.putIntegerArrayList("music", mSongIdList);
        bundle.putInt("musicType", musicType);

        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public BasePresenter getPresenter() {
        return mPresenter;
    }

    @Override
    public void initPresenter() {
        mPresenter = new CollectDialogPresenter();
        mPresenter.attachModelView(new BaseModel(), this);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NO_FRAME, R.style.CustomDatePickerDialog);
        mContext = getContext();
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        initWindows();
        View view = inflater.inflate(R.layout.dialog_collection, container);
        unbinder = ButterKnife.bind(this, view);

        if (getArguments() != null) {
            mSongIdList = getArguments().getIntegerArrayList("music");
            musicType = getArguments().getInt("musicType");
        }

        mPresenter.getUserInfo();
        return view;
    }

    public void initWindows() {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        WindowManager.LayoutParams params = getDialog().getWindow().getAttributes();
        params.gravity = Gravity.CENTER_HORIZONTAL;
        getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        getDialog().getWindow().setAttributes(params);
    }

    @Override
    public void onStart() {
        super.onStart();
        Window window = getDialog().getWindow();
        int dialogWeight = (int) (mContext.getResources().getDisplayMetrics().widthPixels * 0.8);
        if (window != null) {
            window.setLayout(dialogWeight, WindowManager.LayoutParams.WRAP_CONTENT);
        }
        getDialog().setCanceledOnTouchOutside(true);
    }

    @Override
    public void setUserUI(UserInfo userInfo) {
        List<SongMenuList> list = userInfo.getCreateSongMenu();
        mPresenter.setMenuSongNum(list);
    }

    @Override
    public void setCreateListUI(List<SongMenuList> songMenuLists) {
        initCollectionDialog(songMenuLists);
    }

    @Override
    public void setAddMusicUI(Response<BaseInfo> baseInfo) {
        showToast(baseInfo != null
                ? baseInfo.getMsg()
                : "添加本地歌曲成功");
        hideLoading();
        dismiss();
    }

    private void initCollectionDialog(final List<SongMenuList> mData) {
        mRecyclerView.setLayoutManager(new WrapContentLinearLayoutManager(mContext));
        CollectionDialogAdapter dialogAdapter = new CollectionDialogAdapter(mData);
        dialogAdapter.addHeaderView(getCollectionHead(mRecyclerView, mContext,
                view -> showEditDialog(mContext)
        ));


        dialogAdapter.setOnItemClickListener((adapter, view, position) -> {
            SongMenuList menuList = (SongMenuList) adapter.getItem(position);
            if (menuList != null) {
                mPresenter.addMusic(menuList.getSongMenuId(), mSongIdList);
            }
        });

        mRecyclerView.setAdapter(dialogAdapter);
    }


    /**
     * 设置歌单列表头部
     */
    private View getCollectionHead(RecyclerView recyclerView, Context context, View.OnClickListener listener) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.dialog_collection_header, (ViewGroup) recyclerView.getParent(), false);
        view.setOnClickListener(listener);
        return view;
    }

    /**
     * @param context 创建歌单
     */
    private void showEditDialog(final Context context) {
        EditDialog editDialog = new EditDialog(context, name -> {
            LogUtils.d(musicType);
            mPresenter.createSongMenu(name, mSongIdList, musicType);
        });
        editDialog.show();
    }
}
